import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    noCache: true                if set true, the page will no be cached(default is false)
    affix: true                  if set true, the tag will affix in the tags-view
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
    {
        path: "/login",
        component: () => import("@/views/Login/index.vue"),
        name: "Login",
        meta: {
            title: "登录",
        },
        hidden: true,
    },
];
export const endRouter = [
    {
        path: "/404",
        component: () => import("@/views/Error-page/404.vue"),
        hidden: true,
    },
    // 404 page must be placed at the end !!!
    { path: "*", redirect: "/404" },
];

import Layout from "@/layout";

// https://webpack.js.org/guides/dependency-management/#requirecontext
const modulesFiles = require.context("./modules", true, /\.js$/);
// 调用所有的路由，不需要单独因为路由文件调用了
const autoRoutes = modulesFiles.keys().reduce((routes, modulePath) => {
    let routeValue = modulesFiles(modulePath).default;
    if (Array.isArray(routeValue)) {
        routes = routes.concat(routeValue);
    } else {
        routes.push(routeValue);
    }
    return routes;
}, []);
for (let i = 0; i < autoRoutes.length; i++) {
    if (!autoRoutes[i].hidden) {
        autoRoutes[i].component = Layout;
    }
}

// 单独引入单独的路由文件，如果文件多了，不方便引入注册使用
/* import { asyncRoutes } from "./asyncRoutes"
for (let i = 0; i < asyncRoutes.length; i++) {
    asyncRoutes[i].component = Layout
} */

// 所有的路由数据
// let allRoutes = constantRoutes.concat(asyncRoutes, endRouter);

let allRoutes = constantRoutes.concat(autoRoutes, endRouter);

export const dynamicRoutes = autoRoutes;

// 防止连续点击多次路由报错
const routerPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
    return routerPush.call(this, location).catch((err) => err);
};

const createRouter = () =>
    new VueRouter({
        // mode: 'history', // require service support
        mode: "hash", // require service support
        scrollBehavior: () => ({ y: 0 }),
        //   routes: constantRoutes
        routes: allRoutes,
    });

const router = createRouter();

export default router;
